<template>
  <a-card title="基本信息" :bordered="false">
    <BasicForm @register="baseRegister" />
  </a-card>
  <a-card title="生成信息" :bordered="false">
    <BasicForm @register="genRegister" />
  </a-card>
  <a-card title="额外参数" :bordered="false" style="height: 500px">
    <BasicForm @register="paramsRegister">
      <template #params="{ model, field }">
        <monaco-editor ref="monacoEditor" v-model="model[field]" lang="json" />
      </template>
    </BasicForm>
  </a-card>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form';
  import { getFormConfig } from './config';
  import { DetailsResultModel } from '../api/model';
  import monacoEditor from '/@/components/Custom/MonacoEditor/index.vue';
  import { Card } from 'ant-design-vue';

  export default defineComponent({
    components: {
      BasicForm,
      monacoEditor,
      [Card.name]: Card,
    },
    props: {
      tableInfo: Object,
    },
    setup(props, _) {
      const tableInfo = ref<DetailsResultModel>({
        items: [],
        tables: [],
      });
      //基础信息
      const [baseRegister, { validate: baseValidate, setFieldsValue: setBaseInfo }] = useForm({
        labelWidth: 100,
        baseColProps: {
          span: 10,
          offset: 1,
        },
        schemas: getFormConfig('base'),
        showActionButtonGroup: false,
      });
      //基础信息
      const [paramsRegister, { validate: paramsValidate, getFieldsValue: getParamsValue, setFieldsValue: setParams }] = useForm({
        labelWidth: 100,
        baseColProps: {
          span: 10,
          offset: 1,
        },
        schemas: [
          {
            label: '额外参数',
            field: 'params',
            colSlot: 'params',
            component: 'Input',
            defaultValue: '{}',
            colProps: {
              span: 24,
            },
          },
        ],
        showActionButtonGroup: false,
      });
      //生成信息
      const [genRegister, { validate: genValidate, setFieldsValue: setGenInfo, updateSchema: updateGenSchema, removeSchemaByFiled: removeGenSchema, appendSchemaByField: appendGenSchema }] = useForm({
        labelWidth: 100,
        baseColProps: {
          span: 6,
          offset: 1,
        },
        schemas: getFormConfig('gen', {
          treeOptions:
            (props?.tableInfo?.items.map((value) => {
              return {
                label: `${value.columnName}:${value.columnComment}`,
                value: value.columnName,
              };
            }) as LabelValueOptions) || [],
          tableInfo: props?.tableInfo,
        }),
        showActionButtonGroup: false,
      });

      onMounted(() => {
        init();
      });
      const init = async () => {
        if (props.tableInfo) {
          tableInfo.value = props.tableInfo;
          await setBaseInfo(props.tableInfo.info);
          await setGenInfo(props.tableInfo.info);
          await setParams(props.tableInfo.info);
        }
      };

      return {
        tableInfo,
        baseRegister,
        baseValidate,
        setBaseInfo,
        genRegister,
        genValidate,
        paramsRegister,
        setParams,
        paramsValidate,
        getParamsValue,
        updateGenSchema,
        removeGenSchema,
        appendGenSchema,
      };
    },
  });
</script>
